﻿//树控件
(function ($, ibpm) {
    "use strict";
    $.BPM_tree = {
        render: function ($self) {
            var dfop = $self[0]._Mtree.dfop;
            var $search = $('<input type="text" id="M_tree_Search' + dfop.id + '" value="" style="display:block; padding:4px; border-radius:4px; border:1px solid silver; width:90%;">');
            if (dfop.hasSearch) {
                $self.append($search);
            }
            else {
                $search = false;
            }

            var $treeRoot = $('<div id="M_tree' + dfop.id + '"></div>');
            $treeRoot[0].nodeClick = dfop.nodeClick;
            $treeRoot[0].nodeCheck = dfop.nodeCheck;
            $treeRoot[0].SClick = dfop.SClick;
            $treeRoot[0].RootId = "M_tree" + dfop.id;
            $treeRoot[0].threeState = dfop.threeState;

            if (dfop.url) {
                if (!dfop.isCheckbox) {
                    $treeRoot.jstree({
                        "core": {
                            "multiple": dfop.multiple,
                            "animation": 0,
                            "themes": {
                                "theme": "default",
                                "dots": true,
                                "icons": dfop.icons
                            },
                            'data': {
                                'url': function () {
                                    return dfop.url;
                                }
                            }
                        },
                        "force_text": true,
                        plugins: ["search"],
                    });
                    //点击事件
                    if (dfop.nodeClick) {
                        if (dfop.isLeaf) {
                            $treeRoot.on('changed.jstree', function (event, obj) {
                                //alert(1);
                                if (!!obj.node.children && obj.node.children.length < 1) {
                                    if (obj.node.parent == "") {
                                        //alert("不是叶节点");
                                    }
                                    else {
                                        if ($treeRoot[0].nodeClick) {
                                            $treeRoot[0].nodeClick(obj.node.id, obj.node.text, obj.node);
                                        }
                                        if ($treeRoot[0].SClick) {
                                            $treeRoot[0].SClick(obj.node.id, obj.node.text, $treeRoot[0].RootId);
                                        }
                                        //执行域到了jsTree上了，脱离了主上下文，所以dfop内存丢失了
                                        //dfop.nodeClick(obj.node.id, obj.node.text);
                                    }
                                }
                                else {
                                    //alert("不是叶节点");
                                }
                            });
                        }
                        else {
                            $treeRoot.on('changed.jstree', function (event, obj) {
                                if ($treeRoot[0].nodeClick) {
                                    $treeRoot[0].nodeClick(obj.node.id, obj.node.text, obj.node);
                                }
                                if ($treeRoot[0].SClick) {
                                    $treeRoot[0].SClick(obj.node.id, obj.node.text, $treeRoot[0].RootId);
                                }
                            });
                        }
                    }
                }
                else {
                    $treeRoot.jstree({
                        "core": {
                            "check_callback": true,
                            "multiple": dfop.multiple,
                            "animation": 0,
                            "themes": {
                                "theme": "default",
                                "dots": true,
                                "icons": dfop.icons
                            },
                            'data': {
                                'url': function () {
                                    return dfop.url;
                                }
                            }
                        },
                        "force_text": true,
                        ui: {
                            theme_name: "checkbox"
                        },
                        plugins: ["search", "checkbox"],
                        "checkbox": {
                            "keep_selected_style": false,
                            "three_state": dfop.threeState,
                            "tie_selection": false
                        },
                    });

                    if (dfop.nodeCheck) {
                        if (dfop.multiple) {
                            if (dfop.isLeaf) {
                                $treeRoot.on('check_node.jstree', function (event, obj) {
                                    var ref = $treeRoot.jstree(true);
                                    var nodes = ref.get_checked();
                                    var dbLs = [];
                                    $.each(nodes, function (i, nd) {
                                        var n = ref.get_node(nd);
                                        if (!!n.children && n.children.length < 1) {
                                            if (obj.node.parent != '') {
                                                dbLs.push({ id: nd, text: n.text });
                                            }
                                        }
                                    });
                                    if ($treeRoot[0].nodeCheck) {
                                        $treeRoot[0].nodeCheck(dbLs, obj.node);
                                    }
                                    //dfop.nodeCheck(dbLs); //原因同上
                                });
                            }
                            else {
                                $treeRoot.on('check_node.jstree', function (event, obj) {
                                    var ref = $treeRoot.jstree(true);
                                    //ref.select_node(obj); //选中，这里选中与勾选是有区别的
                                    var nodes = ref.get_checked();  //这个是勾选
                                    var dbLs = [];
                                    $.each(nodes, function (i, nd) {
                                        var n = ref.get_node(nd);
                                        dbLs.push({ id: nd, text: n.text, icon: '' });
                                    });
                                    if ($treeRoot[0].threeState) {
                                        $(".jstree-undetermined").each(function () {
                                            var nid = $(this).parent().parent().attr('id');
                                            var ntxt = ref.get_text(nid);
                                            dbLs.push({ id: nid, text: ntxt, icon: 'undetermined' });
                                        });
                                    }
                                    if ($treeRoot[0].nodeCheck) {
                                        $treeRoot[0].nodeCheck(dbLs, obj.node);
                                    }
                                });
                            }
                        }
                        else {
                            if (dfop.isLeaf) {
                                $treeRoot.on('check_node.jstree', function (event, obj) {
                                    var ref = $treeRoot.jstree(true);
                                    var nodes = ref.get_checked();
                                    $.each(nodes, function (i, nd) {
                                        if (nd != obj.node.id)
                                            ref.uncheck_node(nd);
                                    });
                                    if (!!obj.node.children && obj.node.children.length < 1) {
                                        if (obj.node.parent != "") {
                                            var dbLs = [];
                                            dbLs.push({ id: obj.node.id, text: obj.node.text });
                                            if ($treeRoot[0].nodeCheck) {
                                                $treeRoot[0].nodeCheck(dbLs, obj.node);
                                            }
                                        }
                                    }
                                });
                            }
                            else {
                                $treeRoot.on('check_node.jstree', function (event, obj) {
                                    var ref = $treeRoot.jstree(true);
                                    //ref.select_node(obj); //选中，这里选中与勾选是有区别的
                                    var nodes = ref.get_checked();  //这个是勾选
                                    $.each(nodes, function (i, nd) {
                                        if (nd != obj.node.id)
                                            ref.uncheck_node(nd);
                                    });
                                    var dbLs = [];
                                    dbLs.push({ id: obj.node.id, text: obj.node.text });
                                    if ($treeRoot[0].nodeCheck) {
                                        $treeRoot[0].nodeCheck(dbLs, obj.node);
                                    }
                                });
                            }
                        }
                    }
                }
            }
            else {
                // 渲染树
                if (!dfop.isCheckbox) {
                    $treeRoot.jstree({
                        "core": {
                            "multiple": dfop.multiple,
                            "animation": 0,
                            "themes": {
                                "theme": "default",
                                "dots": true,
                                "icons": dfop.icons
                            },
                            data: dfop.data
                        },
                        "force_text": true,
                        plugins: ["search"],
                    });
                    if (dfop.nodeClick) {
                        if (dfop.isLeaf) {
                            $treeRoot.on('changed.jstree', function (event, obj) {
                                if (!!obj.node.children && obj.node.children.length < 1) {
                                    if (obj.node.parent == "") {
                                    }
                                    else {
                                        if ($treeRoot[0].nodeClick) {
                                            $treeRoot[0].nodeClick(obj.node.id, obj.node.text, obj.node);
                                        }
                                        if ($treeRoot[0].SClick) {
                                            $treeRoot[0].SClick(obj.node.id, obj.node.text, $treeRoot[0].RootId);
                                        }
                                    }
                                }
                                else {
                                }
                            });
                        }
                        else {
                            $treeRoot.on('changed.jstree', function (event, obj) {
                                if ($treeRoot[0].nodeClick) {
                                    $treeRoot[0].nodeClick(obj.node.id, obj.node.text, obj.node);
                                }
                                //alert($treeRoot[0].RootId);
                                if ($treeRoot[0].SClick) {
                                    $treeRoot[0].SClick(obj.node.id, obj.node.text, $treeRoot[0].RootId);
                                }
                            });
                        }
                    }
                }
                else {
                    $treeRoot.jstree({
                        "core": {
                            "check_callback": true,
                            "multiple": dfop.multiple,
                            "animation": 0,
                            "themes": {
                                "theme": "default",
                                "dots": true,
                                "icons": dfop.icons
                            },
                            data: dfop.data
                        },
                        "force_text": true,
                        ui: {
                            theme_name: "checkbox"
                        },
                        plugins: ["search", "checkbox"],
                        "checkbox": {
                            "keep_selected_style": false,
                            "three_state": dfop.threeState,
                            "tie_selection": false
                        },
                    });

                    if (dfop.nodeCheck) {
                        if (dfop.multiple) {
                            if (dfop.isLeaf) {
                                $treeRoot.on('check_node.jstree', function (event, obj) {
                                    var ref = $treeRoot.jstree(true);
                                    var nodes = ref.get_checked();
                                    var dbLs = [];
                                    $.each(nodes, function (i, nd) {
                                        var n = ref.get_node(nd);
                                        if (!!n.children && n.children.length < 1) {
                                            if (obj.node.parent != '') {
                                                dbLs.push({ id: nd, text: n.text });
                                            }
                                        }
                                    });
                                    if ($treeRoot[0].nodeCheck) {
                                        $treeRoot[0].nodeCheck(dbLs, obj.node);
                                    }
                                });
                            }
                            else {
                                $treeRoot.on('check_node.jstree', function (event, obj) {
                                    var ref = $treeRoot.jstree(true);
                                    //ref.select_node(obj); //选中，这里选中与勾选是有区别的
                                    var nodes = ref.get_checked();  //这个是勾选
                                    var dbLs = [];
                                    $.each(nodes, function (i, nd) {
                                        var n = ref.get_node(nd);
                                        dbLs.push({ id: nd, text: n.text, icon: '' });
                                    });
                                    if ($treeRoot[0].threeState) {
                                        $(".jstree-undetermined").each(function () {
                                            var nid = $(this).parent().parent().attr('id');
                                            var ntxt = ref.get_text(nid);
                                            dbLs.push({ id: nid, text: ntxt, icon: 'undetermined' });
                                        });
                                    }
                                    if ($treeRoot[0].nodeCheck) {
                                        $treeRoot[0].nodeCheck(dbLs, obj.node);
                                    }
                                });
                            }
                        }
                        else {
                            if (dfop.isLeaf) {
                                $treeRoot.on('check_node.jstree', function (event, obj) {
                                    var ref = $treeRoot.jstree(true);
                                    var nodes = ref.get_checked();
                                    $.each(nodes, function (i, nd) {
                                        if (nd != obj.node.id)
                                            ref.uncheck_node(nd);
                                    });
                                    if (!!obj.node.children && obj.node.children.length < 1) {
                                        if (obj.node.parent != "") {
                                            var dbLs = [];
                                            dbLs.push({ id: obj.node.id, text: obj.node.text });
                                            if ($treeRoot[0].nodeCheck) {
                                                $treeRoot[0].nodeCheck(dbLs, obj.node);
                                            }
                                        }
                                    }
                                });
                            }
                            else {
                                $treeRoot.on('check_node.jstree', function (event, obj) {
                                    var ref = $treeRoot.jstree(true);
                                    //ref.select_node(obj); //选中，这里选中与勾选是有区别的
                                    var nodes = ref.get_checked();  //这个是勾选
                                    $.each(nodes, function (i, nd) {
                                        if (nd != obj.node.id)
                                            ref.uncheck_node(nd);
                                    });
                                    var dbLs = [];
                                    dbLs.push({ id: obj.node.id, text: obj.node.text });
                                    if ($treeRoot[0].nodeCheck) {
                                        $treeRoot[0].nodeCheck(dbLs, obj.node);
                                    }
                                });
                            }
                        }
                    }
                }
            }

            if ($search) {
                var to = false;
                $search.keyup(function () {
                    if (to) {
                        clearTimeout(to);
                    }
                    to = setTimeout(function () {
                        var v = $search.val();
                        $treeRoot.jstree(true).search(v);
                    }, 250);
                });
            }

            $self.append($treeRoot);
            $self.ibpm_scroll();
            dfop = null;
        },
        GoSearch: function (keyword, dfop) {
            //---要延迟执行，等待页面元素加载到位---
            setTimeout(function () {
                if (dfop.hasSearch) {
                    $('#M_tree_Search' + dfop.id + '').attr('value', keyword);
                }
                var ref = $('#M_tree' + dfop.id + '').jstree(true);
                ref.search(keyword);
            }, 300);
        },
        getCheck: function (dfop) {
            var dbLs = [];
            if (dfop.isCheckbox) {
                var ref = $('#M_tree' + dfop.id + '').jstree(true);
                var nodes = ref.get_checked();
                $.each(nodes, function (i, nd) {
                    var n = ref.get_node(nd);
                    dbLs.push({ id: nd, text: n.text, icon: '' });
                });
                if (dfop.threeState) {
                    $(".jstree-undetermined").each(function () {
                        var nid = $(this).parent().parent().attr('id');
                        var ntxt = ref.get_text(nid);
                        dbLs.push({ id: nid, text: ntxt, icon: 'undetermined' });
                    });
                }
            }
            return dbLs;
        },
        setCheck: function (list, dfop) {
            if (!!list.length) {
                if (list.length < 1)
                    return;
            }
            else
                return;

            setTimeout(function () {
                if (dfop.isCheckbox) {
                    var ref = $('#M_tree' + dfop.id + '').jstree(true);
                    $.each(list, function (id, item) {
                        ref.check_node(item);
                    });
                }
            }, 300);
        },
        allCheck: function (dfop) {
            setTimeout(function () {
                if (dfop.isCheckbox) {
                    var ref = $('#M_tree' + dfop.id + '').jstree(true);
                    ref.check_all();
                }
            }, 300);
        },
        allNoCheck: function (dfop) {
            setTimeout(function () {
                if (dfop.isCheckbox) {
                    var ref = $('#M_tree' + dfop.id + '').jstree(true);
                    ref.uncheck_all();
                }
            }, 300);
        },
    };
    $.fn.ibpm_tree = function (settings) {
        var dfop = {
            data: [],
            url: false,
            isCheckbox: false,
            hasSearch: false,
            icons: false,
            threeState: false, //是否进行父子节点级联选择
            isLeaf: true,  //是否只取叶节点
            // 是否多选
            multiple: false,
            // 点击事件（节点信息）,节点$对象
            nodeClick: false,
            // 选中事件（节点信息）,节点$对象
            nodeCheck: false,
            // 响应组件事件
            SClick: false
        };
        $.extend(dfop, settings);
        var $self = $(this);
        dfop.id = $self.attr("id");
        if (dfop.id == null || dfop.id == "") {
            dfop.id = "Mtree" + new Date().getTime();
            $self.attr("id", dfop.id);
        }
        $self.html('');
        $self[0]._Mtree = { dfop: dfop };
        $.BPM_tree.render($self);
        dfop = null;
        return $self;
    };
    $.fn.ibpm_treeSet = function (name, op) {
        var $self = $(this);
        var dfop = $self[0]._Mtree.dfop;

        switch (name) {
            case 'allNoCheck':
                $.BPM_tree.allNoCheck(dfop);
                break;
            case 'allCheck':
                $.BPM_tree.allCheck(dfop);
                break;
            case 'setCheck':
                var list = op;
                $.BPM_tree.setCheck(list, dfop);
                break;
            case 'getCheckNodes':
                return $.BPM_tree.getCheck(dfop);
            case 'search':  //显示查询                
                $.BPM_tree.GoSearch(op.keyword, dfop);
                break;
            case 'refresh': //重新刷新
                $.extend(dfop, op || {});
                $self.ibpm_tree(dfop);
                break;
        }
    };
})(jQuery, top.ibpm);